package cn.touna.jss.modules.third.dianhuabang.entity;

import cn.touna.risk.api.contraints.IdCard;
import cn.touna.risk.api.contraints.Mobile;
import cn.touna.risk.api.contraints.facet.First;
import cn.touna.jss.common.util.DateUtil;
import cn.touna.jss.modules.third.dianhuabang.utils.TimestampUtil;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 电话帮请求
 */
public class DianHuaBangRequestEntity {
    /**
     * 用户手机号，必传
     */
    @Mobile(message = "手机校验失败", groups = {First.class})
    private String mobile;

    /**
     * 身份证号
     */
    @IdCard(message = "身份证校验失败", groups = {First.class})
    private String idCard;
    /**
     * 用户姓名
     */
    @NotNull(message = "用户姓名为空", groups = {First.class})
    private String name;

    /**
     * 时间戳(秒级), 催收分析计算的基准时间，如果上传此参数，则一定正确传入秒级时间戳 （长度为10位）
     */
    private Long time;

    /**
     * 是否使用系统时间，可选值：1和2，缺省默认1。1：查询时间time参数缺省时，使用系统当前时间；2：查询时间time缺省时，使用最后一次通话时间
     */
    private Integer time_type;

    private List<CallLogEntity> call_log;

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getTime() {
        return time == null ? time() : TimestampUtil.getOffsetTimestamp(time, 10);
    }

    public void setTime(Long time) {
        this.time = time;
    }

    public Integer getTime_type() {
        return (time_type == null) ? 1 : time_type;
    }

    public void setTime_type(Integer time_type) {
        this.time_type = time_type;
    }

    public List<CallLogEntity> getCall_log() {
        return call_log;
    }

    public void setCall_log(List<CallLogEntity> call_log) {
        this.call_log = call_log;
    }

    /**
     * 当time缺省时，根据time_type给出time
     *
     * @return
     */
    private Long time() {
        Integer timeType = getTime_type();
        if (timeType == 1) {
            time = DateUtil.getTimestamp(new Date(), 10);
        } else if (timeType == 2) {
            Long lastTime = -1L;
            for (CallLogEntity callLogEntity : call_log) {
                if (callLogEntity.getCall_time() > lastTime) {
                    lastTime = callLogEntity.getCall_time();
                }
            }
            time = lastTime;
        }
        return time;
    }
}
